package ex.mlist;

import java.util.ArrayList;
import java.util.List;

import Database.DBHelper;
import Database.Ringtone;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;

public class Scheduler extends ListActivity{
	/** Called when the activity is first created. */
	
	 AlertDialog.Builder alert;
	 ListView profiles;
	 List<Integer> profiles_list ;
	 DBHelper db;
	 int count;
	 int profile_id;
   @Override
   public void onCreate(Bundle savedInstanceState) {
   	
       super.onCreate(savedInstanceState);
       setContentView(R.layout.scheduler);
       
       db=new DBHelper(Scheduler.this);
       
       registerForContextMenu(getListView());
      
       profiles_list = new ArrayList<Integer>();
       profiles = getListView();
       
     
      
       
       refreshList();
       
       
    
  }
 
   @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
                                   ContextMenuInfo menuInfo) {
     super.onCreateContextMenu(menu, v, menuInfo);
     MenuInflater inflater = getMenuInflater();
     inflater.inflate(R.menu.context_menu, menu);
   }
   
   public boolean onContextItemSelected(MenuItem item) {
	   AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	   switch (item.getItemId()) {
	   case R.id.apply:
	     apply(info.position);
	     return true;
	   case R.id.delete:
	     cancel( info.position);
	     return true;
	   default:
	     return super.onContextItemSelected(item);
	   }
	 }
   
   private void apply(int position) 
   {
	// TODO Auto-generated method stub
	   
	   SharedPreferences tones = getSharedPreferences("MYLIST", 0);
		SharedPreferences.Editor editor = tones.edit();
		editor.putInt("profile_id",position);
		editor.commit();
	
   }

public void setButtonHandler(View V)
   {
	   
	   db.openDB();
	   	profile_id=db.getProfilesCount();
	   	db.close();
	   	profile_id+=1;
	   	System.out.println(profile_id);
   	Intent intent=new Intent(Scheduler.this, SetTime.class);
   	intent.putExtra("profile_id", profile_id);
		startActivity(intent);	
		
		
		
   }
   
  
   
   protected void onListItemClick(ListView l, View v, int position, long id) {
		System.gc();
		//Toast.makeText(this,name,5).show();
		Intent intent=new Intent(Scheduler.this,ProfileActivity.class);
		
		intent.putExtra("profile_id",profiles_list.get(position).intValue() );
		
	
		startActivity(intent);
	}
	
   private void refreshList() {
   	
   	db.openDB();
   	profiles_list=db.getProfiles();
   	db.close();
    profiles.setAdapter(new ProfileAdapter(getApplicationContext(),profiles_list));
       
	}


   public void cancel(int number)
	{
		db.openDB();
		db.deleteProfile(profile_id);
		db.close();
		
	}



	
   
  

}
